package net.huashitong.Indicators.app;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.filter.IndustryFilter;
import net.huashitong.Indicators.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sirdc.modules.core.web.model.Message;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Controller
@RequestMapping(value = "app/industry")
public class IndustryController extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private IndustryService service;

    /**
     * 企业行业首页
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Message index(){
        List<Map<String,Object>> resList = new ArrayList<>();
        List<Industry> list = service.getCatalog();
        List<String> titleList = new ArrayList<>();
        List<String> codeList = new ArrayList<>();
        for(Industry industry : list){
            if(!codeList.contains(industry.getIndustryCode())){
                codeList.add(industry.getIndustryCode());
            }
            if(!titleList.contains(industry.getTitle())){
                titleList.add(industry.getTitle());
            }
        }
        for(String str:codeList){
            Map<String,Object> map =new HashMap<>();
            List<Map<String,Object>> titles = new ArrayList<>();

            List<String> flagList = new ArrayList<>();

            for(Industry industry : list){
                Map<String,Object> titleMap = new HashMap<>();
                if(str.equals(industry.getIndustryCode())){
                    if(!map.containsKey("industryCode")){
                        map.put("industryCode",industry.getIndustryCode());
                    }
                    if(!map.containsKey("industryName")){
                        map.put("industryName",industry.getIndustryName());
                    }
                    if(!flagList.contains(industry.getTitle())){

                        String titleName = industry.getTitle();
                        String year = industry.getDataTime().substring(0,4)+"年";
                        String ban = "";
                        String month = "";
                        if("Y".equals(industry.getFrequency())){
                            if("12".equals(industry.getDataTime().substring(4,6))){
                                ban = "下半年";
                            }else{
                                ban = "上半年";
                            }
                        }
                        if("M".equals(industry.getFrequency())){
                            if("0".equals(industry.getDataTime().substring(4,5))){
                                month = "1-"+industry.getDataTime().substring(5,6)+"月";
                            }else{
                                month = "1-"+industry.getDataTime().substring(4,6)+"月";
                            }
                        }
                        titleMap.put("titleName","Y".equals(industry.getFrequency())?
                                year+ban+titleName
                                :year+month+titleName);
                        titleMap.put("titleCode",industry.getTitle());
                        titleMap.put("haveData",industry.getHaveData());
                        titleMap.put("frequency", industry.getFrequency());
                        titles.add(titleMap);
                        flagList.add(industry.getTitle());
                    }
                }
            }
            map.put("title",titles);
            resList.add(map);
        }

        return coverMessage("200", "获取行业目录成功",resList);
    }

    /**
     * 企业行业二级页面
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDetailByTitle", method = RequestMethod.POST)
    public Message queryDetailByTitle(@RequestBody IndustryFilter filter) {

        List<Industry> industries = service.queryIndustry(filter);
        List<Map<String,String>> resIndus = new ArrayList<>();
        Map<String,Object> resMap = new HashMap<>();
        List<String> timeList = new ArrayList<>();
        if(industries.size()>0){
            for (Industry industry : industries) {
                if (!timeList.contains(industry.getDataTime())) {
                    timeList.add(industry.getDataTime());
                }
            }

            for (Industry industry : industries) {
                Map<String,String> map = new HashMap<>();
                if(timeList.get(0).equals(industry.getDataTime())){

                    String titleName = industry.getTitle();
                    String year = industry.getDataTime().substring(0,4)+"年";
                    String ban = "";
                    String month = "";
                    if("Y".equals(industry.getFrequency())){
                        if("12".equals(industry.getDataTime().substring(4,6))){
                            ban = "下半年";
                        }else{
                            ban = "上半年";
                        }
                    }
                    if("M".equals(industry.getFrequency())){
                        if("0".equals(industry.getDataTime().substring(4,5))){
                            month = "1-"+industry.getDataTime().substring(5,6)+"月";
                        }else{
                            month = "1-"+industry.getDataTime().substring(4,6)+"月";
                        }
                    }
                    map.put("sysId",industry.getSysId());
                    map.put("industryCode",industry.getIndustryCode());
                    map.put("industryName",industry.getIndustryName());
                    map.put("title","Y".equals(industry.getFrequency())?
                            year+ban+titleName
                            :year+month+titleName);
                    map.put("ranks",industry.getRanks());
                    map.put("companyName",industry.getCompanyName());
                    map.put("dataTime",industry.getDataTime());
                    map.put("haveData",industry.getHaveData());
                    map.put("frequency",industry.getFrequency());
                    map.put("rankData",industry.getRankData());
                    map.put("itemName", StringUtils.isBlank(industry.getItemName())?"":industry.getItemName());
                    resIndus.add(map);
                }
            }
            resMap.put("frequency", industries.get(0).getFrequency());
            resMap.put("allDataTime", timeList);
            resMap.put("list", resIndus);
            resMap.put("dataTime", timeList.get(0));
        }
        return coverMessage("200", "获取排名结果成功", resMap);
    }

    /**
     * 企业行业三级数据
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detailByTitle", method = RequestMethod.POST)
    public Message DetailByTitle(@RequestBody IndustryFilter filter) {
        Map industries = service.getByIndustry(filter);
        return coverMessage("200", "获取排名结果成功", industries);

    }

}
